//
//  DGAccountManager.m
//  DGAccountKit
//
//  Created by Jinxiao on 11/19/13.
//  Copyright (c) 2013 debugeek. All rights reserved.
//

#import "DGAccountManager.h"
#import "DGAccount.h"
#import "DGAccountStore.h"
#import "DGAccountError.h"

NSString * const DGCurrentAccountIdentifier = @"DGCurrentAccountIdentifier";

@interface DGAccountManager ()
@property (nonatomic, readwrite, strong) DGAccount *currentAccount;
@end

@implementation DGAccountManager

+ (instancetype)sharedInstance
{
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = self.new;
    });
    return instance;
}

- (void)reloadCurrentAccount
{
    NSString *identifier = [[NSUserDefaults standardUserDefaults] stringForKey:DGCurrentAccountIdentifier];
    if(identifier)
    {
        self.currentAccount = [[DGAccountStore sharedInstance] accountByIdentifier:identifier];
    }
    else
    {
        NSArray *accounts = [[DGAccountStore sharedInstance] accounts];
        if([accounts count] > 0)
        {
            self.currentAccount = [accounts objectAtIndex:0];
        }
        else
        {
            self.currentAccount = nil;
        }
    }
}

- (void)setCurrentAccount:(DGAccount *)currentAccount
{
    if(![currentAccount validateAccount])
    {
        _currentAccount = nil;
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:DGCurrentAccountIdentifier];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else if(![currentAccount isCurrentAccount])
    {
        _currentAccount = currentAccount;
        
        if(!self.currentAccount.credential)
        {
            self.currentAccount.credential = [[DGAccountCredential alloc] init];
            self.currentAccount.credential.userID = self.currentAccount.userID;
        }
        
        if(!self.currentAccount.credential.token)
        {
            [self.currentAccount.credential loadCredentials];
        }
            
        [[NSUserDefaults standardUserDefaults] setObject:self.currentAccount.identifier forKey:DGCurrentAccountIdentifier];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)switchAccount:(DGAccount *)account completion:(DGAccountManagerSwitchAccountCompletion)completion
{
    if(!account && !self.currentAccount)
    {
        if(completion)
        {
            completion(NO, [NSError errorWithDomain:DGAccountErrorDomain code:DGAccountErrorUnknown userInfo:nil]);
        }
        
        return;
    }
    else if([account isCurrentAccount])
    {
        if(completion)
        {
            completion(YES, nil);
        }
        
        return;
    }
    else
    {
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        
        if(self.currentAccount)
        {
            [userInfo setObject:self.currentAccount forKey:DGAccountOldAccountKey];
        }
        
        if(account)
        {
            [userInfo setObject:account forKey:DGAccountNewAccountKey];
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:DGCurrentAccountWillChangeNotification object:self userInfo:userInfo];
        
        self.currentAccount = account;
        
        if(completion)
        {
            completion(YES, nil);
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:DGCurrentAccountDidChangeNotification object:self userInfo:userInfo];
    }
}


@end
